/**根据code从服务器取出对应代码表记录*/
function getCodeDesc(table_name,code_id,where_clause,pNotFoundMessage){
	//alert(contextPath);
   var codeArray = new Array();
   if ( code_id==""){
   		codeArray[0] = "";
       	codeArray[1] = "";
       	codeArray[2] = "";
       	return codeArray;
    }
   if ( table_name==""  ){
       window.alert("请输入代码表的表名！");
       return null;
   }
   var sTableName = table_name.toUpperCase();
   var whereClause = where_clause;
   try{
	 $.ajax({
	  url: url+'/servlet/AjaxServlet?action=loadTable&tableName=' +sTableName +'&code='+code_id+'&whereClause='+whereClause,
      method: "get",
      async: false,
      dataType: "json",
      success: function(msg) {
           if (msg==""||msg==null) {
              window.alert(pNotFoundMessage);
              return null;
            }  else {
            	codeArray = msg[0];	
            }
        }
     });
     
    }catch(e){
		window.alert ("读取代码表数据出错，错误信息为："+e.description);
		return null;
	}
	return codeArray;

}




/**
 *display code description in DescTextBox by CodeTextBox's value,return Id in idtextbox
 *code description data is in parameter
 *@param pIdTextbox:the Id of the ID textbox, String Type
 *@param pCodeTextbox:the Id of the Code textbox,String Type
 *@param pDescTextbox:the Id of the Desc textbox,String Type
 *@param pCodeArray:a two dimension array that contains the id,code and description data
 *@param pNotFoundMessage:when input code not found in the CodeDescArray,then alert this message
 */
 function showDescWithArray(pIdTextBox,pCodeTextBox,pDescTextBox,pArrayData,pNotFoundMessage){
    var valId = document.getElementById(pIdTextBox);
    var valCode = document.getElementById(pCodeTextBox);
    var valDesc = document.getElementById(pDescTextBox);
    if(window.event.srcElement.value==""||window.event.srcElement.value.strip()==""){
      valId.value="";
      valCode.value="";
      valDesc.value="";
      return true;
    }
    var arrayData = eval('('+pArrayData+')');
    for( i=0;i<arrayData.length;i++){
    	if(window.event.srcElement.value==arrayData[i][2]){
            valId.value=arrayData[i][0];
    		valDesc.value=arrayData[i][1];
    		return true;
    	}
    }
//not found,alert and set focus;
    alert(pNotFoundMessage);
    valId.value="";
    valDesc.value="";
    return false;
 }
 
 /**
 *display code description in textbox2 by textbox1's value
 *code description data is in the server
 *@param pIdTextboxName:the id of the id textbox, String Type
 *@param pCodeTextboxName:the id of the code textbox,String Type
 *@param pDescTextboxName:the id of the name textbox,String Type
 *@param pTableName:the code table name that contains the code description data
 *@param pNotFoundMessage:when input code not found in the CodeDescArray,then alert this message
 */
 function showDescFromServer(pIdTextboxName,pCodeTextboxName,pDescTextboxName,pTableName,pWhereClause,pNotFoundMessage){
    //根据ID取出页面对象
    var valId = document.getElementById(pIdTextboxName);
    var valCode = document.getElementById(pCodeTextboxName);
	var valDesc = document.getElementById(pDescTextboxName);
	var valWhereClause = pWhereClause;
    if(valCode.value==""){
      valId.value="";
      valCode.value="";
      valDesc.value="";
      return true;
    }
    var needFocus = false;
    //调用AJAX方法从服务器取出代码表数据
    var aryCode = getCodeDesc(pTableName,valCode.value,pWhereClause,pNotFoundMessage);
    if (aryCode==null||aryCode=="") {
        valId.value="";
        valDesc.value="";
        return true;
    }
    var Id=aryCode[0];
    var desc=aryCode[1];
    var code=aryCode[2];
    if(desc!=null){
    	if((desc.indexOf("Msg Not Found!")==0)||(desc.indexOf("Error:Code not found!")==0)){
    		alert(pNotFoundMessage);
			needFocus = true;
    	}else if(desc.indexOf("Error:")==0){
    		alert(desc);
			needFocus = true;
    	}else if(desc==""){
			needFocus = true;
    	}
    }else{
    	alert("发生错误：服务器返回空数据！");
    	needFocus=true;
    }

    valId.value=Id;
    valCode.value=code;
    valDesc.value=desc;

    if(needFocus){
       valId.value="";
       valDesc.value="";
       return false;
    }else{
      return true;
    }
 }
